package estest;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import utils.ESClientUtil;

import java.util.HashMap;
import java.util.Map;

public class test {
    @Test
    public void test1() throws Exception{
        //1.获取链接
        TransportClient client = ESClientUtil.getClient();
        //指定索引，类型，id 来得到可以新增的对象
        IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1");
        //创建一个hashmap 来存入新增的数据
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("id",1L);
        map.put("name","柯基");
        map.put("age",21);
        map.put("sex",1);

        IndexRequestBuilder source = builder.setSource(map);
        //获取设置的值
        IndexResponse getSource = source.get();
        System.out.println(getSource);
        client.close();
    }
    @Test
    public void test2() throws Exception{
        TransportClient client = ESClientUtil.getClient();

        for (int i = 0; i < 100 ; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id" , i);
            map.put("name" , "柯基" + i);
            map.put("age" , i);
            map.put("sex" , i % 2);
            IndexResponse response = client.prepareIndex("pethome", "pet", i + "").setSource(map).get();
            System.out.println(response);
        }
        client.close();

    }
    @Test
    public void test3() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        SearchRequestBuilder builder = client.prepareSearch("pethome");
        // 3.设置搜索条件
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchQuery("name","柯基"));
        boolQuery.filter(QueryBuilders.termQuery("sex",1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(10));

        // 一个方法要求传一个接口，有几种方式可以传，ID越高，越推荐使用：1.匿名内部类 2.实现类 3.接口对应的工具类
        builder.setQuery(boolQuery);
        // 4.设置分页排序 （页码-1）*每页展示条数
        builder.setFrom(0);
        //设置分页数
        builder.setSize(5);
        //设置倒序
        builder.addSort("age", SortOrder.DESC);
        SearchResponse response = builder.get();
        SearchHit[] hits = response.getHits().getHits();
        for (SearchHit hit : hits) {
            //循环遍历，并打印转成map 格式的数据
            System.out.println(hit.getSourceAsMap());
        }
        client.close();
    }
    @Test
    public void test04() {
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();

        //创建索引
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "pet", "1");
        //添加数据
        Map<String,Object> data = new HashMap<String, Object>();
        data.put("id",1);
        data.put("name","kj");
        data.put("sex",1);
        //获取结果
        IndexResponse indexResponse = indexRequestBuilder.setSource(data).get();
        //打印信息
        System.out.println(indexResponse);
        //关闭链接
        client.close();
    }
}
